package com.android.kotlincoroutinemvvmdemo.shader;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/*
* 点  线  三角形渲染器
* */
public class DotLineTriangleRenderer implements GLSurfaceView.Renderer {

    private static final int POSITION_ONE_POINT = 1;
    private static final int POSITION_LINES = 2;
    private static final int POSITION_TRIANGLE = 3;
    private static final int POSITION_SQUARE = 4;

    //点
    private final FloatBuffer vertexBufferPoint;
    //三角形
    private final FloatBuffer vertexBufferTriangle;
    //线
    private final FloatBuffer vertexBufferLine;
    //矩形
    private final FloatBuffer vertexBufferSquare;
    private final FloatBuffer vertexBufferSquareIndices;


    private final FloatBuffer triangleColorBuffer;
    private final FloatBuffer lineColorBuffer;

    private int mProgram;

    /**
     * 点的坐标
     */
//    private float[] vertexPoints = new float[]{
//            0.0f, 0.5f, 0.0f,
//            -0.5f, -0.5f, 0.0f,
//            0.5f, -0.5f, 0.0f
//    };

    //直角三角形1
//    private float[] vertexPoints = new float[] {
//            0.0f, 1f, 0.0f,
//            0f, 0f, 0.0f,
//            1f, 0f, 0f
//    };

    //直角三角形2
    // x - 0.2f 表示往左平移0.2个单位
    // y - 0.2f 表示往下平移0.2个单位
    private float[] vertexPointsTriangle = new float[] {
            //x                y               z
            0.0f - 0.2f,     1.0f - 0.2f,     0.0f,
            0.0f - 0.2f,     0.0f - 0.2f,     0.0f,
            1.0f - 0.2f,     0.0f - 0.2f,     0.0f
    };

    //直角三角形2
    // x - 0.6f 表示往左平移0.6个单位
    // y - 0.3f 表示往下平移0.3个单位
//    private float[] vertexPoints = new float[] {
//            //  x               y            z
//            0.0f - 0.6f,   1.0f - 0.3f,     0.0f,
//            0.0f - 0.6f,   0.0f - 0.3f,     0.0f,
//            1.0f - 0.6f,   0.0f - 0.3f,     0.0f
//    };

    //绘制一个点  也即一个矩形(默认是正方形)
    private float[] vertexPoints = new float[] {
            //  x               y            z
            0.0f - 0.8f,   1.0f - 0.3f,     0.0f,
    };

    //绘制线
    private float[] vertexLines = new float[] {
            //  x               y            z
            0.0f - 0.8f,   1.0f - 0.3f,     0.0f,
            0.0f,          1.0f,            0.0f,
    };

    //矩形
    private float[] vertexSquare = new float[] {
            //  x               y            z
            0.0f,              0.5f,        0.0f,
            0.0f,             -0.6f,        0.0f,
            0.6f,             -0.6f,        0.0f,
            0.6f,             0.5f,         0.0f,
    };

    private float[] vertexSquareIndices = new float[]{
            0, 1, 2,
            0, 2, 3
    };

    /**
     * 顶点着色器
     */
    private String vertextShader =
            "#version 300 es \n"
                    + "layout (location = 0) in vec4 vPosition;\n"
                    + "layout (location = 1) in vec4 aColor;\n"
                    + "out vec4 vColor;\n"
                    + "void main() { \n"
                    + "gl_Position  = vPosition;\n"
                    + "gl_PointSize = 50.0;\n"
                    + "vColor = aColor;\n"
                    + "}\n";

    private String fragmentShader =
            "#version 300 es \n"
                    + "precision mediump float;\n"
                    + "in vec4 vColor;\n"
                    + "out vec4 fragColor;\n"
                    + "void main() { \n"
                    //+ "vColor = vec4(0.0,1.0,1.0,1.0);\n"
                    + "fragColor = vec4(0.0,1.0,1.0,1.0);\n"
                    //+ "fragColor = vColor; \n"
                    + "}\n";

    private float triangle_color[] = {
            0.0f, 1.0f, 0.0f, 1.0f,
            1.0f, 0.0f, 0.0f, 1.0f,
            0.0f, 0.0f, 1.0f, 1.0f
    };

    private float line_color[] = {
            0.0f, 1.0f, 0.0f, 1.0f,
            1.0f, 0.0f, 0.0f, 1.0f,
    };

    public DotLineTriangleRenderer() {
        //---------------------图形start-----------------------//
        //分配内存空间,每个浮点型占4字节空间(点)
        vertexBufferPoint = ByteBuffer.allocateDirect(vertexPoints.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        //传入指定的坐标数据
        vertexBufferPoint.put(vertexPoints);
        vertexBufferPoint.position(0);

        //分配内存空间,每个浮点型占4字节空间(三角形)
        vertexBufferTriangle = ByteBuffer.allocateDirect(vertexPointsTriangle.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        //传入指定的坐标数据
        vertexBufferTriangle.put(vertexPointsTriangle);
        vertexBufferTriangle.position(0);

        //分配内存空间,每个浮点型占4字节空间(线)
        vertexBufferLine = ByteBuffer.allocateDirect(vertexLines.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        //传入指定的坐标数据
        vertexBufferLine.put(vertexLines);
        vertexBufferLine.position(0);

        //分配内存空间,每个浮点型占4字节空间(矩形)
        vertexBufferSquare = ByteBuffer.allocateDirect(vertexSquare.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        //传入指定的坐标数据
        vertexBufferSquare.put(vertexSquare);
        vertexBufferSquare.position(0);

        vertexBufferSquareIndices = ByteBuffer.allocateDirect(vertexSquareIndices.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        //传入指定的坐标数据
        vertexBufferSquareIndices.put(vertexSquareIndices);
        vertexBufferSquareIndices.position(0);
        //---------------------图形end-----------------------//


        //---------------------颜色填充start-----------------------//
        triangleColorBuffer = ByteBuffer.allocateDirect(triangle_color.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        //传入指定的数据
        triangleColorBuffer.put(triangle_color);
        triangleColorBuffer.position(0);


        lineColorBuffer = ByteBuffer.allocateDirect(line_color.length * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        //传入指定的数据
        lineColorBuffer.put(line_color);
        lineColorBuffer.position(0);
        //---------------------颜色填充end-----------------------//
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        //设置背景颜色
        GLES30.glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
        //编译
        final int vertexShaderId = ShaderUtils.compileVertexShader(vertextShader);
        final int fragmentShaderId = ShaderUtils.compileFragmentShader(fragmentShader);
        //鏈接程序片段
        mProgram = ShaderUtils.linkProgram(vertexShaderId, fragmentShaderId);
        //在OpenGLES环境中使用程序片段
        GLES30.glUseProgram(mProgram);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES30.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);

        //------------------绘制点--------------------//
        //启用顶点的句柄
        //GLES30.glEnableVertexAttribArray(0);
        //准备坐标数据
        //GLES30.glVertexAttribPointer(0, POSITION_ONE_POINT, GLES30.GL_FLOAT, false, 0, vertexBufferPoint);
        //进行绘制(点--小矩形)
        //GLES30.glDrawArrays(GLES30.GL_POINTS, 0, POSITION_ONE_POINT);
        //------------------绘制点--------------------//

        //------------------三角形--------------------//
        //GLES30.glEnableVertexAttribArray(0);
        //GLES30.glVertexAttribPointer(0, POSITION_TRIANGLE, GLES30.GL_FLOAT, false, 0, vertexBufferTriangle);
        //着色
        //GLES30.glEnableVertexAttribArray(1);
        //GLES30.glVertexAttribPointer(1, POSITION_TRIANGLE + 1, GLES30.GL_FLOAT, false, 0, triangleColorBuffer);
        //GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, POSITION_TRIANGLE);
        //------------------三角形--------------------//

        //------------------线--------------------//
        //GLES30.glEnableVertexAttribArray(0);
        //GLES30.glVertexAttribPointer(0, POSITION_LINES, GLES30.GL_FLOAT, false, 0, vertexBufferLine);
        //着色
        //GLES30.glEnableVertexAttribArray(1);
        //GLES30.glVertexAttribPointer(1, POSITION_LINES + 1, GLES30.GL_FLOAT, false, 0, lineColorBuffer);
        //GLES30.glDrawArrays(GLES30.GL_LINES, 0, POSITION_LINES);
        //------------------线--------------------//

        //------------------矩形--------------------//
        GLES30.glEnableVertexAttribArray(0);
        //填充数据
        //GLES30.glVertexAttribPointer(0, 3, GLES30.GL_FLOAT, false, 0, vertexBufferSquare);
        //进行绘制
        //GLES30.glDrawElements(GLES30.GL_TRIANGLES, vertexSquareIndices.length, GLES30.GL_FLOAT, vertexBufferSquareIndices);
        //GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, POSITION_SQUARE);
        //------------------矩形--------------------//


        //禁止顶点数组的句柄
        GLES30.glDisableVertexAttribArray(0);
        //GLES30.glDisableVertexAttribArray(1);
    }
}
